import {
	mapState
} from "vuex";
import w_table from "@/components/base/w_table.vue";
import w_s_table from "@/components/base/w_s_table.vue";
import w_pagination from "@/components/base/w_pagination.vue";
import w_form_modal from "@/components/base/w_form_modal.vue";
import w_detail_modal from '@/components/base/w_detail_modal.vue'

export default {
	components: {
		w_table,
		w_s_table,
		w_pagination,
		w_form_modal,
		w_detail_modal,
	},
	data() {
		return {
			api_config: {},
			table_columns: [],
			table_row_actions: [],
			form_fields: [],
			form_fields_default_value: {},
			form_field_disabled_map: {},
			form_field_multiple_map: {},
			form_field_input_append_map: {},
			radio_options_map: {},
			select_config_map: {},
			tree_config_map: {},
			upload_config_map: {},
			upload_file_list: [],
			hidden_fields_in_table: [],
			hidden_fields_in_form: [],
			hidden_fields_in_detail: [],
			//基础配置


			//
			//分页搜索
			keyword: '',
			pagination: {
				page: 1,
				pageNum: 10,
			},
			total: 0,
			//分页搜索
			tableData: [], //
		};
	},
	computed: {
		...mapState([
			//
		]),
	},

	filters: {

	},
	created() {
		this.setPageConfig()

		this.initTableConfig()
		this.query_select_config()
		this.query_tree_config()
		this.setView();
	},
	mounted() {

	},
	methods: {
		setPageConfig() {
			console.log('this.page_config', this.page_config)
			if (this.page_config) {
				this.api_config = this.page_config.api_config || {};
				this.table_columns = this.page_config.table_columns || [];
				this.table_row_actions = this.page_config.table_row_actions || [];
				this.form_fields = this.page_config.form_fields || [];
				this.form_fields_default_value = this.page_config.form_fields_default_value || {};
				this.form_field_disabled_map = this.page_config.form_field_disabled_map || {};
				this.form_field_multiple_map = this.page_config.form_field_multiple_map || {};
				this.form_field_input_append_map = this.page_config.form_field_input_append_map || {};
				this.radio_options_map = this.page_config.radio_options_map || {};
				this.select_config_map = this.page_config.select_config_map || {};
				this.tree_config_map = this.page_config.tree_config_map || {};
				this.upload_config_map = this.page_config.upload_config_map || {};
				this.upload_file_list = this.page_config.upload_file_list || [];
				this.hidden_fields_in_table = this.page_config.hidden_fields_in_table || [];
				this.hidden_fields_in_form = this.page_config.hidden_fields_in_form || [];
				this.hidden_fields_in_detail = this.page_config.hidden_fields_in_detail || [];
			} else {
				this.page_config = {
					api_config: this.api_config,
					table_columns: this.table_columns,
					table_row_actions: this.table_row_actions,
					form_fields: this.form_fields,
					form_fields_default_value: this.form_fields_default_value,
					form_field_disabled_map: this.form_field_disabled_map,
					form_field_multiple_map: this.form_field_multiple_map,
					form_field_input_append_map: this.form_field_input_append_map,
					radio_options_map: this.radio_options_map,
					select_config_map: this.select_config_map,
					tree_config_map: this.tree_config_map,
					upload_config_map: this.upload_config_map,
					upload_file_list: this.upload_file_list,
					hidden_fields_in_table: this.hidden_fields_in_table,
					hidden_fields_in_form: this.hidden_fields_in_form,
					hidden_fields_in_detail: this.hidden_fields_in_detail,
				}
			}
		},

		query_select_config() {
			let config_map = this.select_config_map
			if (config_map) {
				for (var key in config_map) {
					let config_item = config_map[key];

					this.query_select_item(config_item)
				}
			}
		},

		query_select_item(config_item) {
			let {
				url,
				method,
				data,
			} = config_item.api || {}
			this.$api({
				url: url,
				method: method,
				data: {
					...data,
				},
			}).then((res) => {
				if (res.code == 200) {
					this.$log('查询select 数据', res.data)
					if (config_item.resHasPagination) {
						config_item.dataList = res.data.list;
					} else {
						config_item.dataList = res.data;
					}
				}
			});
		},



		query_tree_config() {
			if (this.tree_config_map) {
				for (var key in this.tree_config_map) {
					let tree_config = this.tree_config_map[key];

					this.query_tree_item(tree_config)
				}
			}
		},

		query_tree_item(tree_config) {
			let {
				url,
				method,
				data,
			} = tree_config.api || {}
			this.$api({
				url: url,
				method: method,
				data: {
					...data,
				},
			}).then((res) => {
				if (res.code == 200) {
					this.$log('查询tree 数据', res.data)
					if (tree_config.resHasPagination) {
						tree_config.treeData = res.data.list;
					} else {
						tree_config.treeData = res.data;
					}
				}
			});
		},
		// 如果分类列表被修改 需要同时刷新可能使用的tree数据
		refreshTreeApi() {
			let has_list_api_in_tree = false;
			let list_action = this.api_config['list']['action']
			if (this.tree_config_map) {
				for (var key in this.tree_config_map) {
					let curr_action = this.tree_config_map[key]['api']['data']['action']
					if (curr_action == list_action) {
						let tree_config = this.tree_config_map[key];
						this.query_tree_item(tree_config)
					}
				}
				// this.tree_config_map[list_action]
			}
		},



		setView() {
			let currentPage=sessionStorage.getItem('currentPage')
			console.log('initQuery-------')
			this.pagination.page = currentPage?currentPage:1;
			let {
				url,
				method,
				action,
				resHasPagination,
				apiParams,
			} = this.api_config['list']
			this.$api({
				url: url,
				method: method,
				data: {
					action: action,
					...apiParams,
					...this.searchParams,
					...this.pagination,
				},
			}).then((res) => {
				if (res.code == 200) {
					let data = res.data;

					if (typeof resHasPagination == 'undefined' || resHasPagination) {
						this.total = data.count;
						let list = data.list;
						if (list[0]) {
							let keys = Object.keys(list[0])
							let form = {}
							keys.forEach(key => {
								form[key] = ''
							})
							console.log('row 空对象', JSON.stringify(form))
						}

						if (this.format_list_data) {
							this.format_list_data(list)
							// list.forEach(v => {
							// 	v.status = parseInt(v.status) || 0
							// })
						} else {
							list.forEach(v => {
								v.status = parseInt(v.status) || 0
							})
						}

						this.tableData = list;
					} else {
						this.total = data.length;
						let list = data
						list.forEach(v => {
							v.status = parseInt(v.status) || 0
						})
						this.tableData = list;
					}

					
					this.$log('表格数据', this.tableData)
					this.initTableData()
				}
			});
		},



		initQuery() {
			this.setView();
		},

		resetSearch() {
			// if(this.reset_searchParams) {
			// 	for(var key in this.reset_searchParams) {
			// 		this.searchParams[key] = this.reset_searchParams[key]
			// 	}
			// }
		},

		// 分页
		onSizeChange(value) {
			this.pagination.pageNum = value;
			this.setView();
		},
		onCurrentChange(value) {
			this.pagination.page = value;
			this.setView();
		},
		doSearch() {
			this.pagination.page = 1;
			this.resetSearch()
			this.setView();
		},

		//表格操作
		emitTable(option) {
			this.$log('处理表格行操作 option', option)
			let {
				action_item,
				table_row
			} = option
			let action_title = action_item.title;
			let action_type = action_item.action_type;

			if (action_type == 'custom') {
				this.doRowOther(table_row, action_title);
			} else if (action_title == '编辑') {
				this.doRowEdit(table_row);
			} else if (action_title == '删除') {
				this.doRowDelete(table_row);
			} else if (action_title == '详情') {
				this.doRowDetail(table_row);
			} else if (action_title == '更多') {
				this.doRowMore(table_row);
			} else {
				this.doRowOther(table_row, action_title);
			}
		},

		// 初始化表格数据
		initTableData() {
			this.$nextTick(() => {
				if (this.$refs.w_table) {
					let option = {
						tableData: this.tableData,
					}
					this.$refs.w_table.initTableData(option);
				} else if (this.$refs.w_table_tree) {
					let option = {
						tableData: this.tableData,
					}
					this.$refs.w_table_tree.initTableData(option);
				} else if (this.$refs.w_s_table) {
					let option = {
						tableData: this.tableData,
					}
					this.$refs.w_s_table.initTableData(option);
				}
			})
		},

		// 初始化配置
		initTableConfig() {
			this.$nextTick(() => {
				if (this.$refs.w_table) {
					let option = {
						table_columns: this.table_columns,
						table_row_actions: this.table_row_actions,
						radio_options_map: this.radio_options_map
					}
					this.$refs.w_table.initTableConfig(option);
				} else if (this.$refs.w_table_tree) {
					let option = {
						table_columns: this.table_columns,
						table_row_actions: this.table_row_actions,
						radio_options_map: this.radio_options_map
					}
					this.$refs.w_table_tree.initTableConfig(option);
				} else if (this.$refs.w_s_table) {
					let option = {
						table_columns: this.table_columns,
						table_row_actions: this.table_row_actions,
						radio_options_map: this.radio_options_map
					}
					this.$refs.w_s_table.initTableConfig(option);
				}
			})
		},

		// 初始化弹窗
		initFormModal() {
			if (this.form_fields) {
				this.$log('form_fields', this.form_fields)
			}

			let option = this.page_config || {
				api_config: this.api_config,
				form_fields: this.form_fields,
				form_fields_default_value: this.form_fields_default_value,
				form_field_disabled_map: this.form_field_disabled_map,
				form_field_multiple_map: this.form_field_multiple_map,
				form_field_input_append_map: this.form_field_input_append_map,
				radio_options_map: this.radio_options_map,
				select_config_map: this.select_config_map,
				tree_config_map: this.tree_config_map,
				upload_config_map: this.upload_config_map,
				upload_file_list: this.upload_file_list,
			}
			this.$refs.w_form_modal.initConfig(option);
		},
		doAdd() {
			this.initFormModal()
			this.$refs.w_form_modal.initAdd();
		},
		doRowEdit(row) {
			this.initFormModal()
			this.$refs.w_form_modal.initEdit(row);
		},
		doRowDelete(row) {
			this.$confirm('确认删除该条记录?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.doRowDeleteConfirm(row);
			}).catch(() => {

			});
		},
		doRowDeleteConfirm(row) {
			let {
				url,
				method,
				action,
				id_field,
			} = this.api_config['delete'];

			this.$api({
				url: url,
				method: method,
				data: {
					action: action,
					[id_field]: row[id_field],
					...this.params_delete
				},
			}).then((res) => {
				if (res.code == 200) {
					this.setView();
					this.$message({
						message: res.msg,
						type: 'success'
					})
				} else {
					alertErr(res.msg)
				}
			});
		},

		//详情
		doRowDetail(row) {
			let option = this.page_config || {
				api_config: this.api_config,
				form_fields: this.form_fields,
				form_fields_default_value: this.form_fields_default_value,
				form_field_disabled_map: this.form_field_disabled_map,
				form_field_multiple_map: this.form_field_multiple_map,
				form_field_input_append_map: this.form_field_input_append_map,
				radio_options_map: this.radio_options_map,
				select_config_map: this.select_config_map,
				tree_config_map: this.tree_config_map,
				upload_config_map: this.upload_config_map,
				upload_file_list: this.upload_file_list,
				hidden_fields_in_detail: this.hidden_fields_in_detail,
			}

			this.$refs.w_detail_modal.initConfig(option);
			this.$refs.w_detail_modal.initEdit(row)
		},

		doRowMore(row) {

		},
		confirmFormSubmit() {
			this.refreshTreeApi()
			this.initQuery();
		}
	},
};